package com.woniuxy.lrz.carlproject.controller;

import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.Hutool;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.common.Result;
import com.woniuxy.lrz.carlproject.common.util.Results;
import com.woniuxy.lrz.carlproject.controller.form.approveform.ShopTestForm;
import com.woniuxy.lrz.carlproject.model.Approve;
import com.woniuxy.lrz.carlproject.model.Manager;
import com.woniuxy.lrz.carlproject.service.ApproveService;
import com.woniuxy.lrz.carlproject.service.ManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@RestController
@RequestMapping("/carlproject/manager")
@Api(tags = "管理员接口")
public class ManagerController {

    @Resource
    private ManagerService managerService;
    @Resource
    private RedisTemplate<String,String> rts;
    @Resource
    private ApproveService approveService;

    @GetMapping("/login")
    @ApiOperation("管理员账号密码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "managerName",value = "管理员账号名"),
            @ApiImplicitParam(name="managerPwd",value = "密码")
    })
    public Results managerLogin(@NotBlank(message = "用户名不能为空") String managerName,
                               @NotBlank(message = "密码不能为空") String managerPwd){
        String s = managerService.managerLogin(managerName, managerPwd);
        return Results.instance().setData(s).setMessage("登录成功");

    }

    @GetMapping("/sendPhoneCode")
    @ApiOperation("管理员手机号获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "手机号")
    })
    public Results sendPhoneCode(@NotBlank(message = "手机号不能为空") String phone){
        //验证手机号是否正确
        Manager phone1 = managerService.getOne(new QueryWrapper<Manager>().eq("phone", phone));
        if(phone1==null){
            throw new AppException("手机号输入有误，请重新输入");
        }
        //验证手机号是否已发送
        Set<String> phonesRedis = rts.opsForZSet().range("car:phoneLogin:" + phone, 0, -1);
        if(phonesRedis.size()!=0){
                throw new AppException("验证码已发送，请稍后再试！");
        }
        String code="";
        for(int i=0;i<4;i++){
            int i1 = RandomUtil.randomInt(0, 9);
            code+=(i1+"");
        }
        rts.opsForZSet().add("car:phoneLogin:"+phone,code,0);
        rts.expire("car:phoneLogin:" + phone,60, TimeUnit.SECONDS);
        return Results.instance().setMessage("发送验证码成功").setData(code);

    }

    @GetMapping("/phoneLogin")
    @ApiOperation("管理员账号手机验证码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "手机号"),
            @ApiImplicitParam(name="code",value = "验证码")
    })
    public Results phoneLogin(@NotBlank(message = "手机号不能为空") String phone,
                              @NotBlank(message = "验证码不能为空") String code){
        String s = managerService.phoneLogin(phone, code);
        return Results.instance().setMessage("登录成功").setData(s);
    }

    @GetMapping("/outLogin")
    @ApiOperation("登出")
    public Results outLogin(){
        return Results.instance().setCode(401);
    }

    @GetMapping("/findShopList")
    @ApiOperation("加载门店申请列表")
    public Results findShopList(){
        List<Approve> list = approveService.list(new QueryWrapper<Approve>().eq("status", "ING"));
        return Results.instance().setMessage("加载门店申请列表成功").setData(list);
    }

    @PutMapping("/testShop")
    @ApiOperation("平台审核门店")
    public Results testShop(@RequestBody @Valid ShopTestForm shopTestForm){
        approveService.update(new UpdateWrapper<Approve>().eq("approve_id",shopTestForm.getApproveId())
        .set("status",shopTestForm.getStatus()));
        return Results.instance().setMessage("操作成功");
    }

}
